#!/usr/bin/env python3
"""
启动Celery Worker脚本
专门处理即梦AI视频生成队列
"""

import subprocess
import sys
import os

def start_celery_worker():
    """启动Celery Worker"""
    
    # 设置环境变量
    os.environ.setdefault("PYTHONPATH", ".")
    
    print("🚀 启动Celery Worker - 即梦AI视频生成队列")
    print("=" * 60)
    
    # Celery Worker命令
    # -A: 指定Celery应用
    # -Q: 指定处理的队列
    # --loglevel: 日志级别
    # --concurrency: 并发数（即梦AI限制=1）
    
    cmd = [
        "celery", 
        "-A", "app.celery_app",
        "worker",
        "-Q", "jimeng_video",  # 只处理即梦AI视频队列
        "--loglevel=info",
        "--concurrency=1",     # 即梦AI并发限制=1
        "--pool=solo"          # 单线程池，确保严格串行
    ]
    
    print(f"执行命令: {' '.join(cmd)}")
    print("-" * 60)
    
    try:
        # 启动Celery Worker
        subprocess.run(cmd, check=True)
    except KeyboardInterrupt:
        print("\n👋 Celery Worker已停止")
    except subprocess.CalledProcessError as e:
        print(f"❌ Celery Worker启动失败: {e}")
        sys.exit(1)
    except FileNotFoundError:
        print("❌ 未找到celery命令，请确保已安装celery")
        print("   pip install celery")
        sys.exit(1)

if __name__ == "__main__":
    print("📋 使用说明:")
    print("1. 确保Redis服务已启动")
    print("2. 确保.env文件配置正确")
    print("3. 此Worker专门处理即梦AI视频生成，并发=1")
    print("4. 按Ctrl+C停止Worker")
    print("")
    
    input("按回车键启动Celery Worker...")
    start_celery_worker()